'''

@author: wyndem
@Emil:   wyndem@qq.com
@FileName: noveldao.py
@Time: 2019/3/11 16:54
@Software: PyCharm
@Description: 

'''
import copy
import time

from novel.dao import POOL
from novel.utils.tool import TOOL




def findBySourceNovel(source):
    """
    通过 来源 链接获取小说信息
    :return:
    """
    conn = POOL.connection()
    cursor = conn.cursor()
    sql="""
    SELECT * FROM `bookdb`.`novel` where novel.novel_source=%s
    """
    cursor.execute(sql, (source))
    data=cursor.fetchone()
    cursor.close()
    conn.close()
    return data



def findById(id):
    """
    通过id查询
    :param id:
    :return:
    """
    conn = POOL.connection()
    cursor = conn.cursor()
    sql="""
    SELECT * FROM `bookdb`.`novel` where novel.id=%s
    """
    cursor.execute(sql, (id))
    data=cursor.fetchone()
    cursor.close()
    conn.close()
    return data



def findByIdCharpter(id):
    """
       通过小说id来查询章节，不包含 章节标题和内容
       :param id:
       :return:
    """
    conn = POOL.connection()
    cursor = conn.cursor()
    sql="""
        SELECT novel_info.chapter_no FROM `bookdb`.`novel_info` where novel_info.novel_id=%s
    """
    cursor.execute(sql, (id))
    data=cursor.fetchall()
    cursor.close()
    conn.close()
    return data




def insertNovel(novel,author_id):
    """
    插入小说
    :param novel:  NovelItem  author_id: 作者id
    :return: 小说的id
    """
    conn = POOL.connection()
    cursor = conn.cursor()
    sql = """
                INSERT INTO `bookdb`.`novel`
                    (`novel_id`, `author_id`, `cover`, `introduce`, `novel_source`, `novel_name`, `created_time`, `update_time`,`sentiment`,`collection_number`) 
                    VALUES (%s, %s, %s, %s, %s, %s,%s,%s,0,0)
                """
    cursor.execute(sql,(
                            str(len(novel['chapter']) + int(TOOL.create_uid())),
                            author_id,
                            novel['cover'],
                            novel['introduction'],
                            novel['source'],
                            novel['name'],
                            int(time.time()),
                            int(time.time())
                        ))


    novel_id = cursor.lastrowid
    # 插入小说章节
    chapter_sql = """
              INSERT INTO `bookdb`.`novel_info`(`novel_id`,`chapter_name`,`chapter_no`, `chapter_url`) VALUES (%s,%s,%s, %s)
          """
    param=[]
    for idx, c in enumerate(novel['chapter']):
        param.append([novel_id,c['title'] ,idx + 1, c['linke']])
    if len(param)!=0:
        cursor.executemany(chapter_sql, param)
        conn.commit()
    id=copy.deepcopy(novel_id)
    cursor.close()
    conn.close()
    return id


def refreshTime(id):
    """
    刷新更新时间
    :return:
    """
    sql = """
     UPDATE `bookdb`.`novel` SET `update_time` = %s WHERE `id` = %s
     """

    conn = POOL.connection()
    cursor = conn.cursor()

    cursor.execute(sql, (int(time.time()),id))

    conn.commit()
    cursor.close()
    conn.close()






if __name__ == '__main__':
    # print(findBySourceNovel("http://www.haobqg.com/html/15_15956/"))
    # print(findByIdCharpter(209)[0][-1])
    i=[1,2,3,4,5,6,7]
    print(i[1:-1])